from django.shortcuts import render
from django.views.generic import TemplateView, ListView, View
from django.shortcuts import render,HttpResponse
import os

VIEW_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_DIR = VIEW_DIR + '/extenddd/static'

# Create your views here.
class index(View):
    def get(self, request, *args, **kwargs):
        print 'index'
        print request
        print 'VIEW_DIR'
        print VIEW_DIR

        #return render(request, 'dc/template_sale_dashboard.html', self.data)
        return render(request, 'template_index.html')

class download(View):
    #def get(self, request, *args, **kwargs):
    def get(self, request, *args, **kwargs):
        print request
        print 'class download(View):'
        print kwargs.get('filename')
        fileName = kwargs.get('filename')
        path = STATIC_DIR + '/download/' + fileName
        print 'path'
        print path
        
        try: 
            #file=open('/home/lxp/stepdem/manage.py','rb')  
            file=open(path,'rb')  
            response = HttpResponse(file, content_type='application/vnd.ms-excel')
            response['Content-Disposition'] = 'attachment; filename=' + fileName
            # for jquery file download
            response.set_cookie('fileDownload', 'true')
            response.set_cookie('path', '/')
            return response
        except Exception as e:
            print e
        return HttpResponse('OK')

        '''
        file=open('/home/lxp/stepdem/manage.py','rb')  
        response =StreamingHttpResponse(file)  
        response['Content-Type']='application/octet-stream'  
        response['Content-Disposition']='attachment;filename="example.tar.gz"'  
        return response 
        #return render(request, 'dc/template_sale_dashboard.html', self.data)
        '''



def filedownload(request,filename):
    print request
    print 'kwargs'
    print filename

    #return render(request, 'dc/template_sale_dashboard.html', self.data)
    return HttpResponse('OK')